 $.fn.snake = function(options) {
	 
	 		settings = jQuery.extend({
     				gridSize: 20  				
  			}, options);

	 		/* variable definition */
		   	var currentGrid = [];
			var direction = 'down';
			var context = this[0].getContext('2d');
			var snakeLength = 3; // Length of snake
			var apple = [];
			var score = 0;
			var appleTimerEnd;
			var maxWide;
			var maxHigh; 
			var canvas = [];
			
			canvas[0] = this.width();
			canvas[1] = this.height() - 100;
			
			maxWide = Math.floor((canvas[0] / settings.gridSize) - 1);
			maxHigh = Math.floor((canvas[1] / settings.gridSize) - 1);
						
			/* init */
			
			// Add first snake
			currentGrid[0] = [];
			currentGrid[0][0] = 0;
			currentGrid[0][1] = 0;
			
			// Redraw screen
			redraw();	
			
			// Move automatically every ---
			setInterval(run, 100);
			
			// Show an apple every ----
			var appleTimer = setTimeout(makeApple, 3000);
			
			
			
			function run (){
					if(direction == 'down'){
						moveSquare(0,1);
					}else if(direction == 'up'){
						moveSquare(0,-1);
					}else if(direction == 'left'){
						moveSquare(-1,0);
					}else if(direction == 'right'){
						moveSquare(1,0);
					}
			}
			
			function makeApple(){
				
				appleTimerEnd = setTimeout(hideApple, 3000);
				var rand1 = Math.floor(Math.random()* maxWide); 
				var rand2 = Math.floor(Math.random()* maxHigh); 
				
				for (i=0;i<= currentGrid.length - 1;i++){
					if(rand1 == currentGrid[i][0] & rand2 == currentGrid[i][1]){
						
						rand1 = Math.floor(Math.random()* maxWide); 
						rand2 = Math.floor(Math.random()* maxHigh); 
						
					}
				}
				
				apple[0] = rand1;
				apple[1] = rand2;
				
				console.log(rand2);
				
	
				
			}
			
			function hideApple(){
				
							apple = [];
							setTimeout(makeApple, 5000);
				
				
			}
			
			function drawApple(gridX, gridY){		
				context.fillStyle = '#ff0000';
				context.beginPath();
				context.arc((gridX * settings.gridSize) + (settings.gridSize / 2) , (gridY * settings.gridSize) + (settings.gridSize / 2), (settings.gridSize / 2), 0, Math.PI * 2, false);
				context.fill();
				context.closePath();	
			}
			
			function createSquare(gridX, gridY){
				
	
				
				context.fillStyle = '#00ff00';				
			
				context.beginPath();
				
				// Create gradients
				
	
				

				
				context.moveTo((gridX * settings.gridSize) + (settings.gridSize * 0.3),(gridY * settings.gridSize));
				context.lineTo((gridX * settings.gridSize) + (settings.gridSize * 0.7), (gridY * settings.gridSize));
				context.quadraticCurveTo((gridX * settings.gridSize) + settings.gridSize, (gridY * settings.gridSize), (gridX * settings.gridSize) + settings.gridSize, (gridY * settings.gridSize) + (settings.gridSize * 0.3));
				context.lineTo((gridX * settings.gridSize) + settings.gridSize, (gridY * settings.gridSize) + (settings.gridSize * 0.7));
				context.quadraticCurveTo((gridX * settings.gridSize) + settings.gridSize, (gridY * settings.gridSize) + settings.gridSize, (gridX * settings.gridSize) + (settings.gridSize * 0.7), (gridY * settings.gridSize) + settings.gridSize);
				context.lineTo((gridX * settings.gridSize) + (settings.gridSize * 0.3) , (gridY * settings.gridSize) + settings.gridSize);
				context.quadraticCurveTo((gridX * settings.gridSize), (gridY * settings.gridSize) + settings.gridSize, (gridX * settings.gridSize) , (gridY * settings.gridSize) + (settings.gridSize * 0.7));
				context.lineTo((gridX * settings.gridSize) , (gridY * settings.gridSize) + (settings.gridSize * 0.3));
				context.quadraticCurveTo((gridX * settings.gridSize), (gridY * settings.gridSize), (gridX * settings.gridSize) + (settings.gridSize * 0.3), (gridY * settings.gridSize) );
				
				
				context.fill();
				context.closePath();
				
				
			}
			
			function redraw() {
					context.clearRect (0, 0,  canvas[0], canvas[1] + 100);
					
					context.fillStyle = '#000';
					context.fillRect (0, canvas[1], canvas[0], 100);
					context.fillStyle = '#fff';
					context.font = "38px Arial";
					context.fillText("Score: " + score, 20, canvas[1] + 60);
					
					
					//alert(currentGrid[0][0]); 
					var i=0;
					for (i=0;i<= currentGrid.length - 1;i++){
						createSquare(currentGrid[i][0], currentGrid[i][1]);
					}
					
					if(apple[0]){
					drawApple(apple[0], apple[1]);
					}
			}
			
			$("#add").click(function(){
				
				snakeLength++;
				
			});
			
			function moveSquare(xPlus, yPlus){
					var next = currentGrid.length;
					//alert(next);
					
					if(next > snakeLength - 1){
					
						currentGrid.splice(0,1);
						next--;
						
					}
					
					currentGrid[next] = [];
					currentGrid[next][0] = currentGrid[next - 1][0] + xPlus;
					currentGrid[next][1] = currentGrid[next - 1][1] + yPlus;	
					

					
					
						if(currentGrid[next][0] == (maxWide + 1)){
							currentGrid[next][0]  = 0;
						}
						
						if(currentGrid[next][1] == (maxHigh + 1)){
							currentGrid[next][1] = 0;
						}
						
						if(currentGrid[next][0] < 0 ){
							currentGrid[next][0] = maxWide; 
						}
						
						
						if(currentGrid[next][1] < 0 ){
							currentGrid[next][1] = maxHigh; 
						}
						
					var i=0;
					
					for (i=0;i<= currentGrid.length - 2;i++){
					
						if(currentGrid[next][0] == currentGrid[i][0] & currentGrid[next][1] == currentGrid[i][1]){
							
							alert("die");
								
						}else if(currentGrid[next][0] == apple[0] & currentGrid[next][1] == apple[1] ){
							
							snakeLength++;
							score = score + 10;
							apple = [];
							clearTimeout(appleTimerEnd);
							setTimeout(makeApple, 3000);
							
						}
						
					}
					
						redraw();
						
			}
			
			$(document).keydown(function(event) {
				if (event.keyCode == '40' & direction != 'up') {					
					direction = 'down';
				}else if (event.keyCode == '39' & direction != 'left') {	
					direction = 'right';
				}else if (event.keyCode == '37' & direction != 'right') {	
					direction = 'left';
				}else if (event.keyCode == '38' & direction != 'down') {
					direction = 'up';	
				}	
			});
			
			
			return true;
	   };